package com.jrx.anytxn.transaction.job.chargeoffProcess.step;

import com.jrx.anytxn.transaction.bean.ChargeoffBean;
import com.jrx.anytxn.transaction.mapper.ext.*;
import com.jrx.anytxn.transaction.service.impl.ChargeoffServiceImpl;
import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.batch.item.ItemWriter;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;


@Service
public class ChargeoffWriter implements ItemWriter<ChargeoffBean> {

    private static final Logger logger = LoggerFactory.getLogger(ChargeoffServiceImpl.class);


    @Resource
    private ExtTlChargeoffInfoMapper extTlChargeoffInfoMapper;
    @Resource
    private ExtAmLoanAccountInfoMapper extAmLoanAccountInfoMapper;
    @Resource
    private ExtAmInterestAccrualInfoMapper extAmInterestAccrualInfoMapper;
    @Resource
    private ExtAmAccountMainInfoMapper extAmAccountMainInfoMapper;
    @Resource
    private ExtAmRepaymentPlanInfoMapper extAmRepaymentPlanInfoMapper;
    @Resource
    private ExtTlGlInterfaceInfoMapper extTlGlInterfaceInfoMapper;
    @Resource
    private ExtAmDelinquencyInfoMapper extAmDelinquencyInfoMapper;
    @Resource
    private ExtTlTransactionInfoMapper extTlTransactionInfoMapper;

    @Override
    public void write(List<? extends ChargeoffBean> items) throws Exception {
        if(CollectionUtils.isNotEmpty(items)){
            for(ChargeoffBean info : items){
                if(null != info.getUpdateTlChargeoffInfo()){
                    extTlChargeoffInfoMapper.batchUpdateStatusTlChargeoffInfo(info.getUpdateTlChargeoffInfo());
                }
                if(null != info.getUpdateAmLoanAccountInfo()){
                    extAmLoanAccountInfoMapper.updateByLoanId(info.getUpdateAmLoanAccountInfo());
                }
                if(CollectionUtils.isNotEmpty(info.getUpdateAmInterestAccrualInfoList())){
                    extAmInterestAccrualInfoMapper.batchUpdateInterestAccrualInfoForBatch(info.getUpdateAmInterestAccrualInfoList());
                }
                if(CollectionUtils.isNotEmpty(info.getSaveAcctList())){
                    extAmAccountMainInfoMapper.batchInsertAccount(info.getSaveAcctList());
                }
                if(CollectionUtils.isNotEmpty(info.getUpdateAmAccountMainInfoList())) {
                    extAmAccountMainInfoMapper.batchUpdateAccountForBatch(info.getUpdateAmAccountMainInfoList());
                }
                if(CollectionUtils.isNotEmpty(info.getUpdateAmRepaymentPlanInfoList())) {
                    extAmRepaymentPlanInfoMapper.batchUpdatePlanForBatch(info.getUpdateAmRepaymentPlanInfoList());
                }
                if(CollectionUtils.isNotEmpty(info.getSaveTlGlInterfaceInfoList())) {
                    extTlGlInterfaceInfoMapper.batchInsertSelective(info.getSaveTlGlInterfaceInfoList());
                }
                if(CollectionUtils.isNotEmpty(info.getUpdateAmDelinquencyInfoList())) {
                    extAmDelinquencyInfoMapper.batchUpdateAccountForBatch(info.getUpdateAmDelinquencyInfoList());
                }
                if(CollectionUtils.isNotEmpty(info.getInsertTlTransactionInfoList())){
                    extTlTransactionInfoMapper.batchInsert(info.getInsertTlTransactionInfoList());
                }
            }
        }
    }
}
